<template>
    <div class="container-fluid" v-loading="loading">
        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header"><h4>文章创建</h4></div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-8">
                                <div class="form-group">
                                    <label for="title">标题</label>
                                    <input type="text" class="form-control" v-model="form.title" placeholder="标题..">
                                </div>
                                <div class="form-group">
                                    <label for="summary">摘要</label>
                                    <el-input
                                            type="text"
                                            placeholder="请输入摘要"
                                            v-model="form.describe"
                                            maxlength="100"
                                            :rows="4"
                                            show-word-limit>
                                    </el-input>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label for="imageUrl">视频</label>
                                    <el-upload
                                            class="avatar-uploader"
                                            action="/api/upload"
                                            :show-file-list="false"
                                            accept="image/gif, image/jpeg, image/png"
                                            :on-success="handleImgUrlSuccess"
                                            :before-upload="beforeAvatarUpload">

                                        <img v-if="form.imageUrl" :src="form.imageUrl" class="avatar">
                                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                    </el-upload>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="imageUrl">视频</label>
                            <div>
                                <video id="myVideo" class="video-js" :poster="form.imageUrl" ref="video" controls="controls" @timeupdate="time">
                                    <source :src="form.videoUrl" type="video/mp4">
                                </video>
                            </div>

                        </div>
                        <div class="form-group">
                            <template v-if="form.verifyStatus">
                                <p>
                                    <button type="submit" class="btn btn-primary"
                                            v-on:click="submit(form.verifyStatus,form.pushStatus)">保存
                                    </button>
                                </p>
                                <p>
                                    <button type="submit" class="btn btn-primary"
                                            v-on:click="submit(form.verifyStatus,1)">加推送
                                    </button>
                                </p>
                            </template>
                            <template v-if="!form.verifyStatus">
                                <p>
                                    <button type="submit" class="btn btn-primary"
                                            v-on:click="submit(-1,form.pushStatus)">不通过
                                    </button>
                                </p>
                                <p>
                                    <button type="submit" class="btn btn-primary"
                                            v-on:click="submit(1,form.pushStatus)">保存并通过审核
                                    </button>
                                </p>
                                <p>
                                    <button type="submit" class="btn btn-primary" v-on:click="submit(1,1)">
                                        通过审核并加推送
                                    </button>
                                </p>
                            </template>
                            <p>
                                <button class="btn btn-primary" v-on:click="changeImg()">修改封面图</button>
                                <button class="btn btn-primary" v-on:click="changeImg(true)">指定当前帧生成封面图</button>
                            </p>
                        </div>

                    </div>

                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        name: "VideoEditComponent",
        data() {
            return {
                form: {
                    title: '',
                    describe: '',
                    imageUrl: '',
                    videoUrl: '',
                    verifyStatus:0,
                    pushStatus:0,
                    player: null,
                },
                loading: false,
                id: $('meta[name="id"]').attr('content')
            }
        },
        computed: {
            player() {
                return this.$video(myVideo, {
                    //确定播放器是否具有用户可以与之交互的控件。没有控件，启动视频播放的唯一方法是使用autoplay属性或通过Player API。
                    // controls: true,
                    //自动播放属性,muted:静音播放
                    //autoplay: "muted",
                    //建议浏览器是否应在<video>加载元素后立即开始下载视频数据。
                    //preload: "auto",
                    //设置视频播放器的显示宽度（以像素为单位）
                    width: "250px",
                    //设置视频播放器的显示高度（以像素为单位）
                    height: "250px",

                    fullScreen: {
                        controlBar: true
                    }
                });
            }
        },
        methods: {
            handleImgUrlSuccess: function (response) {
                let self = this;
                if (response.code === 200) {
                    self.form.imageUrl = response.url;
                    this.loading = false;
                }
            },
            beforeAvatarUpload: function (item) {
                this.loading = true;
            },
            async getInfo() {
                let self = this;
                let uri = "article/video/cartoon/"+self.id;
                self.loading = true;
                await axios.get(uri).then(function (result) {
                    self.loading = false;
                    self.form = result.result;
                    console.log(self.form);
                    self.$nextTick(() => {
                        self.player
                    });
                }).catch(function (error) {
                    self.loading = false;
                });
            },
            changeImg(currentTime=false){
                let self = this;
                let uri = "video/change-img/" + self.id;

                if (currentTime) {
                    uri += '/' + (this.player.currentTime());
                }
                self.loading = true;
                axios.get(uri).then(function (result) {
                    self.loading = false;
                    self.form.imageUrl = result.data.imageUrl;
                    console.log(result);
                }).catch(function (error) {
                    self.loading = false;
                });
            },
            submit: function (verifyStatus,pushStatus) {

                var orginalStatus = this.form.verifyStatus;

                let self = this;
                let uri = "video/" + self.id;
                if (!self.form.title) {
                    self.$message.error("必须填写标题");
                    return;
                }
                /*if (!self.form.describe) {
                    self.$message.error("必须填写摘要");
                    return;
                }*/
                if (!self.form.imageUrl) {
                    self.$message.error("必须上传封面图");
                    return;
                }
                if (!self.form.videoUrl) {
                    self.$message.error("必须上传视频");
                    return;
                }
                self.loading = true;
                self.form.pushStatus = pushStatus;
                self.form.verifyStatus = verifyStatus;

                axios.put(uri, self.form).then(function (result) {
                    self.loading = false;
                    if (result) {
                        var url = '/admin/video/index';
                        if (orginalStatus === 0){
                            url = '/admin/video/verify';
                        }
                        window.location.href = url;
                    } else {
                        self.$message.error("修改视频失败！");
                    }
                }).catch(function (error) {
                    self.loading = false;
                });
            },
            time() {
                var currentTime = this.player.currentTime().toFixed(3);
                //this.currentTime = currentTime;
            },
        },
        mounted() {
            this.getInfo();
        }
    }
</script>

<style scoped>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .el-upload__input {
        display: none;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 120px;
        height: 120px;
        line-height: 120px;
        text-align: center;
    }

    .avatar {
        width: 120px;
        height: 120px;
        display: block;
    }
</style>
